package com.ikangtai.shecare.common.util;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;

import java.util.UUID;

/**
 * Created by jerry on 15/12/9.
 * need add <uses-permission android:name="android.permission.READ_PHONE_STATE"/> in
 * AndroidManifest.xml
 */
public class DeviceInfo {
    private TelephonyManager tm;

    private StringBuilder sb = new StringBuilder();
    private String devicedId;
    private String deviceSoftwareVersion;
    private String line1Number;
    private String networkCountryIso;
    private String networkOperator;
    private String networkOperatorName;
    private int networkType;
    private int phoneType;
    private String simCountryIso;
    private String simOperator;
    private String simOperatorName;
    private String simSerialNumber;
    private int simState;
    private String subscriberIdIMSI;
    private String soiceMailNumber;
    private String phoneModel;
    private String phoneVersion;

    public DeviceInfo(Context context){
        tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }

    public String getDevicedId(){
        return (null == tm) ? null : tm.getDeviceId();
    }

    public String getDeviceSoftwareVersion(){
        return (null == tm) ? null : tm.getDeviceSoftwareVersion();
    }

    public String getLine1Number(){
        return (null == tm) ? null : tm.getLine1Number();
    }

    public String getNetworkCountryIso(){
        return (null == tm) ? null : tm.getNetworkCountryIso();
    }

    public String getNetworkOperator(){
        return (null == tm) ? null : tm.getNetworkOperator();
    }

    public String getNetworkOperatorName(){
        return (null == tm) ? null : tm.getNetworkOperatorName();
    }

    public int getNetworkType(){
        return (null == tm) ? null : tm.getNetworkType();
    }

    public int getPhoneType(){
        return (null == tm) ? null : tm.getPhoneType();
    }

    public String getSimCountryIso(){
        return (null == tm) ? null : tm.getSimCountryIso();
    }

    public String getSimOperator(){
        return (null == tm) ? null : tm.getSimOperator();
    }

    public String getSimOperatorName(){
        return (null == tm) ? null : tm.getSimOperatorName();
    }

    public String getSimSerialNumber(){
        return (null == tm) ? null : tm.getSimSerialNumber();
    }

    public int getSimState(){
        return (null == tm) ? null : tm.getSimState();
    }

    public String getSubscriberIdIMSI(){
        return (null == tm) ? null : tm.getSubscriberId();
    }

    public String getVoiceMailNumber(){
        return (null == tm) ? null : tm.getVoiceMailNumber();
    }
    
    public String getPhoneModel(){
    	return android.os.Build.MODEL;
    }
    
    public String getPhoneVersion(){
    	return "android"+android.os.Build.VERSION.RELEASE;
    }

    public String getDeviceInfo(){
        if(null == tm)
            return null;
        else{
            sb.append("\nDeviceId(IMEI) = " + tm.getDeviceId());
            sb.append("\nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());
            sb.append("\nLine1Number = " + tm.getLine1Number());
            sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());
            sb.append("\nNetworkOperator = " + tm.getNetworkOperator());
            sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());
            sb.append("\nNetworkType = " + tm.getNetworkType());
            sb.append("\nPhoneType = " + tm.getPhoneType());
            sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
            sb.append("\nSimOperator = " + tm.getSimOperator());
            sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
            sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());
            sb.append("\nSimState = " + tm.getSimState());
            sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId());
            sb.append("\nVoiceMailNumber = " + tm.getVoiceMailNumber());
            sb.append("\nPhoneModel = " + getPhoneModel());
            sb.append("\nPhoneVersion = " + getPhoneVersion());
            return sb.toString();
        }
    }

    public static String getDeviceID() {
        String phoneID;
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        if (btAdapter != null)
            phoneID = btAdapter.getAddress();
        else
            phoneID = getUniquePsuedoID();

//        else {
//            DeviceInfo deviceInfo = new DeviceInfo(context);
//            phoneID = deviceInfo.getDevicedId();
//        }
//
//        if (phoneID == null) {
//            phoneID = getUniquePsuedoID();
//        }

        return phoneID;
    }

    public static String getUniquePsuedoID() {
        String serial;

        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +

                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +

                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +

                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +

                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +

                Build.USER.length() % 10; //13 位

        try {
            serial = android.os.Build.class.getField("SERIAL").get(null).toString();
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
        //使用硬件信息拼凑出来的15位号码
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }

}
